/*
 * @Author: szx
 * @Date: 2022-06-12 19:38:54
 * @LastEditTime: 2022-06-12 19:47:40
 * @Description:
 * @FilePath: \leetcode\800-899\890\890.js
 */
/**
 * @param {string[]} words
 * @param {string} pattern
 * @return {string[]}
 */
var findAndReplacePattern = function (words, pattern) {
    const res = [];
    for (let word of words) if (match(word, pattern) && match(pattern, word)) res.push(word);
    return res;
};

const match = (word, pattern) => {
    const map = new Map();
    for (let i = 0; i < word.length; i++) {
        const x = word[i],
            y = pattern[i];
        if (!map.has(x)) map.set(x, y);
        else if (map.get(x) != y) return false;
    }
    return true;
};
